package com.cloud.pole.advert.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cloud.pole.advert.biz.AdvertBiz;
import com.cloud.pole.advert.model.entity.Advert;
import com.cloud.pole.advert.service.AdvertService;
import com.cloud.pole.common.biz.BaseBiz;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.rest.BaseController;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("advert/front")
@Slf4j
public class AdvertController extends BaseController<AdvertBiz, Advert> {

	@Autowired
	AdvertService advertService;

	@ResponseBody
	@RequestMapping(value = "/getAdvertList")
	public ApiRestResponse getAdvertList(@RequestParam(defaultValue = "0") Integer advertPosId) {
		try {
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, advertService.getAdvertList(advertPosId));
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}

	@SuppressWarnings("unchecked")
	@GetMapping("bannerList")
	@ResponseBody
	public ApiRestResponse<Advert> list(Integer pageNo, Integer pageSize, Integer shopId) {
		pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
		pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
		try {
			PageInfo<Advert> pageInfo = advertService.selectPageShopGoodsGroupAdminList(pageNo, pageSize, shopId);
			return ApiRestResponse.successOfPage("成功", (int) pageInfo.getTotal(), pageInfo.getPages(),
					pageInfo.getPageSize(), pageInfo.getList());
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
	}

	@PostMapping("bannerAdd")
	@ResponseBody
	public ApiRestResponse<Advert> addBanner(@RequestBody Advert advert) {
		try {
			String token = request.getHeader(CommonConstants.AUTH_USER_TOKEN_HEADER);
			advertService.addBanner(token,advert);
			return ApiRestResponse.success("200", null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}
	}
	
	@GetMapping("getBanneres/{id}")
	@ResponseBody
	public ApiRestResponse<Advert> selectById(@PathVariable("id") Integer advertId){
		try {
			Advert selectById = advertService.selectById(advertId);
			return ApiRestResponse.success("200", selectById);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}
		
	}
	
	@PutMapping("bannerUpdate")
	@ResponseBody
	public ApiRestResponse<Advert> bannerUpdate(@RequestBody Advert advert){
		try {
			advertService.updateBanner(advert);
			return ApiRestResponse.success("200", null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}
	}
	
	@DeleteMapping("bannerDele/{id}")
	@ResponseBody
	public ApiRestResponse<Advert> bannerDelete(@PathVariable("id")Integer advertId){
		try {
			advertService.deleteBanner(advertId);
			return ApiRestResponse.success("200", null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}
	}
	
}
